<?php
	//Trieu: dong nay can thiet cho viec add enviromment va cac bien
	require_once ( '../../../wp-blog-header.php');
?>

<?php
	//Tuy theo ket qua ma cau query khac nhau
	
	$tab = intval($_REQUEST['tablist']); //chon list tab list
	$page = intval($_REQUEST['page']); // kiem tra trang 
	if($page < 1)
		$page = 1;
		
	$post_per_page = get_option('posts_per_page', 10); //so bai post tren moi trang, do admin quy dinh
	$start = intval($post_per_page) * (intval($page) - 1);
	//$end = $start + $post_per_page;
	
	//echo "$start -- $end";
	
	switch ($tab){ //ung voi moi gia tri cua tab se co 1 cau query khac nhau
		case '1':
			//$query = 'posts_per_page='.$post_per_page.'&paged='.$page.'&post_status=publish&orderby=ID&order=DESC';
			$query = "SELECT * FROM $wpdb->posts
       				WHERE post_status='publish' AND post_type = 'post'
					ORDER BY ID DESC
					LIMIT $start, $post_per_page";
			break;
		case '2':
			//$query = 'posts_per_page='.$post_per_page.'&paged='.$page.'&post_status=publish&orderby=comment_cout&order=DESC&orderby=ID&order=DESC';
			$query =  "SELECT * FROM $wpdb->posts
       				WHERE post_status='publish' AND post_type = 'post'
					ORDER BY comment_count DESC, ID DESC
					LIMIT $start, $post_per_page";
			break;
		case '3':
		//	$query  ='posts_per_page='.$post_per_page.'&paged='.$page.'&post_status=publish&comment_cout=0&orderby=ID&order=DESC';
			 $query = "SELECT * FROM $wpdb->posts
       				WHERE post_type = 'post' AND post_status='publish' AND comment_count='0'
					ORDER BY ID DESC
					LIMIT $start, $post_per_page";
			break;
	}
?>

<div id='response'>		
<?php
//*/The Query
//echo $query;
//query_posts($query); //hoac get post

//The Loop
//if ( have_posts() ) : while ( have_posts() ) : the_post();*/
?>

<?php
	$results = $wpdb->get_results($query, OBJECT);
	$num = wp_count_posts()->publish; // Trieu:lay nhung bai post da dc pushlish
?>
<!--<pre>
<?php print_r($results)?>
</pre>
--><div id="sc" class="tabcontainer">  <!-- #tabcontainer (Trieu)chua list -->
<?php foreach ($results as $post):?>
    	 	<?php setup_postdata($post);?>    
	<div class="question"> <!-- #question (Trieu)item cua list danh sach-->
	        <div class="question-title">
	            <div class="question-user-img">
	            <!-- Lay avatar cua user -->
	                <?php
	                $link_avatar = 'wp-content/uploads/'.get_usermeta(get_the_author_ID(), 'avatar');
	                if($link_avatar == '')
	                	$link_avatar = get_bloginfo('template_directory')."/images/no_avatar.gif";
	                	//echo $link_avatar; exit;
	                ?>
	                <img alt="No image" src="<?php echo $link_avatar;?>" height="32" width="32"/>
	                
	                </div>
	                <div class="question-by">
	                <p><?php the_author_posts_link();?></p>
	                    <p class="question-h1"><a style="color:#000000" href='<?php echo the_permalink()?>'><?php the_title()?></a></p>
	                </div>
	                <div style="clear:both;"></div>
	            </div>
		<div class="question-bottom">
			<div class="answer">
				<a href="<?php echo get_permalink();?>">Answer this question</a>
			</div>
			<div class="answers">
				<p> <!-- (Trieu) lay so comment, chia ra 3 truong hop 0, 1 va nhieu comment-->
					<?php comments_number( 
						'<font class="answer-nr">0 </font><a href="' . get_permalink() . '">Answer</a>',
						'<font class="answer-nr">1 </font><a href="' . get_permalink() . '">Answer</a>',
						'<font class="answer-nr">% </font><a href="' . get_permalink() . '">Answers</a>'
					);?>
				</p>
			</div>
			<div class="answer-date">
				<p> <?php the_time('F jS, Y');?> </p>
			</div>
			<div class="tags"> <!-- #tags (Trieu) cac tag lien quan den bai viet -->
				<?php $taglist = get_the_tag_list('<li>', '</li><li>,', '</li>');?>
				<?php if ($taglist != ''):?>
					<p>&laquo;&raquo;</p>
					<ul>
						<?php echo $taglist;?>
					</ul>
				<?php endif;?>
			</div> <!-- //tags -->
		</div>
	</div> <!-- //question -->
<?php 
/*endwhile; 
else:
 	echo "No Result";
endif;
//Reset Query
wp_reset_query();*/
?>
<?php endforeach;?>
</div> <!-- //tabcontainer -->
		
<?php	
	$total = $num / $post_per_page; //so trang mac dinh la 5, co the chon nhieu hon
	if($num % $post_per_page) 
		$total += 1;
	$current = $page;
?>
	
<div class="wp-pagenavi">			
	<p>
		<?php
			for ($i = 1; $i <= $total; $i++){
				if($current == $i):				
			?>
				<span><?php echo $i?></span>
			<?php else:?>
				<a onmouseup="tab_click('<?php echo $tab;?>', '<?php echo $i?>');" href="#" ><?php echo $i?></a>
			<?php 
				endif;
			}
			
		?>
	
	</p>
</div>     
</div> <!-- //response -->



